/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} target
 * @return {number[][]}
 */

var pathSum = function (root, target) {
    if (!root) {
        return []
    }
    let res = []
    let queue = []
    backtrack(root)
    return res.filter(item => {
        return item.reduce((pre, cur) => pre + cur) === target
    })
    function backtrack(root) {
        queue.push(root.val)
        if (!root.left && !root.right) {
            res.push(queue.slice())
            return
        }
        if (root.left) {
            backtrack(root.left)
            queue.pop()
        }
        if (root.right) {
            backtrack(root.right)
            queue.pop()
        }
    }
};